package com.stylefeng.guns.modular.owBackstageMgr.newsCategoryMgr.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.owBackstageMgr.newsCategoryMgr.service.INewsCategoryService;
import com.stylefeng.guns.modular.system.model.NewsCategory;
import com.stylefeng.guns.modular.system.warpper.NewsCategoryWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 新闻/文章类别控制器
 *
 * @author fengshuonan
 * @Date 2018-06-20 07:27:55
 */
@Controller
@RequestMapping("/newsCategory")
public class NewsCategoryController extends BaseController {

    private String PREFIX = "/newsCategoryMgr/newsCategory/";

    @Autowired
    private INewsCategoryService newsCategoryService;

    /**
     * 跳转到类别首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "newsCategory.html";
    }

    /**
     * 跳转到添加类别页面
     */
    @RequestMapping("/newsCategory_add")
    public String newsCategoryAdd() {
        return PREFIX + "newsCategory_add.html";
    }

    /**
     * 跳转到修改类别页面
     */
    @RequestMapping("/newsCategory_update/{newsCategoryId}")
    public String newsCategoryUpdate(@PathVariable Integer newsCategoryId, Model model) {
        NewsCategory newsCategory = newsCategoryService.selectById(newsCategoryId);
        model.addAttribute("item",newsCategory);
        LogObjectHolder.me().set(newsCategory);
        return PREFIX + "newsCategory_edit.html";
    }

    /**
     * 获取文章类别列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name) {
        Page<NewsCategory> page = new PageFactory<NewsCategory>().defaultPage();
        List<Map<String, Object>> result = newsCategoryService.getNewsCategories(page, name,  page.getOrderByField(), page.isAsc());
        page.setRecords((List<NewsCategory>) new NewsCategoryWarpper(result).warp());
        return super.packForBT(page);
        //return newsCategoryService.selectList(null);
    }

    /**
     * 新增文章类别
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(NewsCategory newsCategory) {
        if (ToolUtil.isOneEmpty(newsCategory, newsCategory.getName(), newsCategory.getThemeId())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        newsCategory.setCreateTime(new Date());
        newsCategory.setUpdateTime(new Date());
        newsCategory.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除文章类别
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer newsCategoryId) {
        NewsCategory newsCategory = newsCategoryService.selectById(newsCategoryId);
        if (ToolUtil.isNotEmpty(newsCategory)) {
            newsCategory.setIsDel(1);
            newsCategory.setUpdateTime(new Date());
            newsCategoryService.updateById(newsCategory);
            return SUCCESS_TIP;
        }
        return new ErrorTip(-1,"找不到要删除的记录~");
    }

    /**
     * 修改文章类别
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(NewsCategory newsCategory) {
        if (ToolUtil.isNotEmpty(newsCategory)) {
            newsCategory.setUpdateTime(new Date());
            newsCategoryService.updateById(newsCategory);
            return SUCCESS_TIP;
        }
        return new ErrorTip(-1,"传入的需要更新的对象为空~");
    }

    /**
     * 文章类别详情
     */
    @RequestMapping(value = "/detail/{newsCategoryId}")
    @ResponseBody
    public Object detail(@PathVariable("newsCategoryId") Integer newsCategoryId) {
        return newsCategoryService.selectById(newsCategoryId);
    }


    /**
     * 前台ajax获取全部的文章类别，id做value，name做下拉选项
     */
    @RequestMapping(value="/getAllCategories")
    @ResponseBody
    public String getAllCategories(){
        EntityWrapper<NewsCategory> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("is_del",0);
        List<NewsCategory> newsCategories = newsCategoryService.selectList(entityWrapper);
        return JSON.toJSONString(newsCategories);
    }
}
